package com.action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bean.ComBean;
import com.util.Constant;

public class ComServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ComServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType(Constant.CONTENTTYPE);
		request.setCharacterEncoding(Constant.CHARACTERENCODING);
		HttpSession session = request.getSession();
		ComBean cBean = new ComBean();
		String date2=new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
		String method = request.getParameter("method");
		if(method.equals("addmc")){ 
			 String mname = request.getParameter("mname"); 
			 int flag = cBean.comUp("insert into ssmc(mc)  values('"+mname+"')");
				if(flag == Constant.SUCCESS){ 
					request.setAttribute("message", "操作成功！");
					request.getRequestDispatcher("admin/yao/index.jsp").forward(request, response); 
				}
				else { 
					request.setAttribute("message", "操作失败！");
					request.getRequestDispatcher("admin/yao/index.jsp").forward(request, response); 
				} 
		}
		else if(method.equals("delmc")){ 
			String id = request.getParameter("id");  
			 int flag = cBean.comUp("delete from ssmc where id='"+id+"'");
				if(flag == Constant.SUCCESS){ 
					request.setAttribute("message", "操作成功！");
					request.getRequestDispatcher("admin/yao/index.jsp").forward(request, response); 
				}
				else { 
					request.setAttribute("message", "操作失败！");
					request.getRequestDispatcher("admin/yao/index.jsp").forward(request, response); 
				} 
		}
		else if(method.equals("addStu")){  
			 String xm = request.getParameter("xm");  
			 String xb = request.getParameter("xb");  
			 String ssmc = request.getParameter("ssmc");  
			 String xh = request.getParameter("xh"); 
			 int flag = cBean.comUp("insert into stu(xm,xb,ssmc,xh) values('"+xm+"','"+xb+"','"+ssmc+"','"+xh+"')");
				if(flag == Constant.SUCCESS){ 
					request.setAttribute("message", "操作成功！");
					request.getRequestDispatcher("admin/stu/index.jsp").forward(request, response); 
				}
				else { 
					request.setAttribute("message", "操作失败！");
					request.getRequestDispatcher("admin/stu/index.jsp").forward(request, response); 
				} 
		}
		else if(method.equals("upStu")){  
			String id = request.getParameter("id");  
			 String xm = request.getParameter("xm");  
			 String xb = request.getParameter("xb");  
			 String ssmc = request.getParameter("ssmc");  
			 String xh = request.getParameter("xh"); 
			 int flag = cBean.comUp("update stu set xm='"+xm+"',xb='"+xb+"',ssmc='"+ssmc+"',xh='"+xh+"' where id='"+id+"'");
				if(flag == Constant.SUCCESS){ 
					request.setAttribute("message", "操作成功！");
					request.getRequestDispatcher("admin/stu/index.jsp").forward(request, response); 
				}
				else { 
					request.setAttribute("message", "操作失败！");
					request.getRequestDispatcher("admin/stu/index.jsp").forward(request, response); 
				} 
		}
		else if(method.equals("delStu")){ 
			String id = request.getParameter("id");  
			 String sql="delete from stu where id='"+id+"'";
			 int flag = cBean.comUp(sql);
				if(flag == Constant.SUCCESS){ 
					request.setAttribute("message", "操作成功！");
					request.getRequestDispatcher("admin/stu/index.jsp").forward(request, response); 
				}
				else { 
					request.setAttribute("message", "操作失败！");
					request.getRequestDispatcher("admin/stu/index.jsp").forward(request, response); 
				} 
		}
		else if(method.equals("addCc")){ 
			//String ccmc="";String gmsj="";String jg="";String sccj="";String ssmc="";String bz="";
			 String mlmc = request.getParameter("mlmc");  
			 String cfnr = request.getParameter("cfnr");  
			 String bz = request.getParameter("bz");  
			 int flag = cBean.comUp("insert into wdml(mlmc,cfnr,bz,sj) values('"+mlmc+"','"+cfnr+"','"+bz+"','"+date2+"' )");
				if(flag == Constant.SUCCESS){ 
					request.setAttribute("message", "操作成功！");
					request.getRequestDispatcher("admin/cc/index.jsp").forward(request, response); 
				}
				else { 
					request.setAttribute("message", "操作失败！");
					request.getRequestDispatcher("admin/cc/index.jsp").forward(request, response); 
				} 
		}
		else if(method.equals("upCc")){  
			String id = request.getParameter("id");  
			String mlmc = request.getParameter("mlmc");  
			 String cfnr = request.getParameter("cfnr");  
			 String bz = request.getParameter("bz");  
			 int flag = cBean.comUp("update wdml set mlmc='"+mlmc+"',cfnr='"+cfnr+"',bz='"+bz+"' where id='"+id+"'");
				if(flag == Constant.SUCCESS){ 
					request.setAttribute("message", "操作成功！");
					request.getRequestDispatcher("admin/cc/index.jsp").forward(request, response); 
				}
				else { 
					request.setAttribute("message", "操作失败！");
					request.getRequestDispatcher("admin/cc/index.jsp").forward(request, response); 
				} 
		}
		else if(method.equals("delCc")){ 
			String id = request.getParameter("id");  
			 String sql="delete from wdml where id='"+id+"'";
			 int flag = cBean.comUp(sql);
				if(flag == Constant.SUCCESS){ 
					request.setAttribute("message", "操作成功！");
					request.getRequestDispatcher("admin/cc/index.jsp").forward(request, response); 
				}
				else { 
					request.setAttribute("message", "操作失败！");
					request.getRequestDispatcher("admin/cc/index.jsp").forward(request, response); 
				} 
		}
		
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
		
		else if(method.equals("delWd")){ 
			String id = request.getParameter("id");  
			 String sql="delete from wd where id='"+id+"'";
			 int flag = cBean.comUp(sql);
				if(flag == Constant.SUCCESS){ 
					request.setAttribute("message", "操作成功！");
					request.getRequestDispatcher("admin/bx/index.jsp").forward(request, response); 
				}
				else { 
					request.setAttribute("message", "操作失败！");
					request.getRequestDispatcher("admin/bx/index.jsp").forward(request, response); 
				} 
		}
		else if(method.equals("addSh")){ 
//			String shzt="";String yy="";String sj="";String xx="";String jg="";String jbr="";String clsj=""; 
			 String shzt = request.getParameter("shzt");  
			 String yy = request.getParameter("yy");  
			 String sj = request.getParameter("sj");  
			 String xx = request.getParameter("xx"); 
			 String jg = request.getParameter("jg"); 
			 String jbr = request.getParameter("jbr"); 
			 String clsj = request.getParameter("clsj"); 
			 int flag = cBean.comUp("insert into sh(shzt,yy,sj,xx,jg,jbr,clsj) " +
			 		"values('"+shzt+"','"+yy+"','"+sj+"','"+xx+"','"+jg+"','"+jbr+"','"+clsj+"')");
				if(flag == Constant.SUCCESS){ 
					request.setAttribute("message", "操作成功！");
					request.getRequestDispatcher("admin/sh/index.jsp").forward(request, response); 
				}
				else { 
					request.setAttribute("message", "操作失败！");
					request.getRequestDispatcher("admin/sh/index.jsp").forward(request, response); 
				} 
		}
		else if(method.equals("upSh")){  
			String id = request.getParameter("id");  
			String shzt = request.getParameter("shzt");  
			 String yy = request.getParameter("yy");  
			 String sj = request.getParameter("sj");  
			 String xx = request.getParameter("xx"); 
			 String jg = request.getParameter("jg"); 
			 String jbr = request.getParameter("jbr"); 
			 String clsj = request.getParameter("clsj"); 
			 int flag = cBean.comUp("update sh set shzt='"+shzt+"',yy='"+yy+"',sj='"+sj+"',xx='"+xx+"',jg='"+jg+"',jbr='"+jbr+"',clsj='"+clsj+"' where id='"+id+"'");
				if(flag == Constant.SUCCESS){ 
					request.setAttribute("message", "操作成功！");
					request.getRequestDispatcher("admin/sh/index.jsp").forward(request, response); 
				}
				else { 
					request.setAttribute("message", "操作失败！");
					request.getRequestDispatcher("admin/sh/index.jsp").forward(request, response); 
				} 
		}
		else if(method.equals("delSh")){ 
			String id = request.getParameter("id");  
			 String sql="delete from sh where id='"+id+"'";
			 int flag = cBean.comUp(sql);
				if(flag == Constant.SUCCESS){ 
					request.setAttribute("message", "操作成功！");
					request.getRequestDispatcher("admin/sh/index.jsp").forward(request, response); 
				}
				else { 
					request.setAttribute("message", "操作失败！");
					request.getRequestDispatcher("admin/sh/index.jsp").forward(request, response); 
				} 
		}
		
		////////////////////////////////////////////////////////////////////////////////////////////////////stu报修
		else if(method.equals("addBx")){ 
//			String shzt="";String yy="";String sj="";String xx="";String jg="";String jbr="";String clsj=""; 
			 String bxr = request.getParameter("bxr");  
			 String ssmc = request.getParameter("ssmc");  
			 String sj = request.getParameter("sj");  
			 String xx = request.getParameter("xx");
			 
			 String username=(String)session.getAttribute("user"); 
			 int flag = cBean.comUp("insert into bx(bxr,ssmc,sj,xx,jg,jbr,clsj,stu) " +
			 		"values('"+bxr+"','"+ssmc+"','"+sj+"','"+xx+"','未处理','无','无','"+username+"')");
				if(flag == Constant.SUCCESS){ 
					request.setAttribute("message", "操作成功！");
					request.getRequestDispatcher("admin/bx/index.jsp").forward(request, response); 
				}
				else { 
					request.setAttribute("message", "操作失败！");
					request.getRequestDispatcher("admin/bx/index.jsp").forward(request, response); 
				} 
		}
		else if(method.equals("upBx")){  
			String id = request.getParameter("id");  
			String jg = request.getParameter("jg");  
			 String jbr = request.getParameter("jbr");  
			 String clsj = request.getParameter("clsj");
			 int flag = cBean.comUp("update bx set jg='"+jg+"',jbr='"+jbr+"',clsj='"+clsj+"' where id='"+id+"'");
				if(flag == Constant.SUCCESS){ 
					request.setAttribute("message", "操作成功！");
					request.getRequestDispatcher("admin/bx/index.jsp").forward(request, response); 
				}
				else { 
					request.setAttribute("message", "操作失败！");
					request.getRequestDispatcher("admin/bx/index.jsp").forward(request, response); 
				} 
		}
		else if(method.equals("delBx")){ 
			String id = request.getParameter("id");  
			 String sql="delete from bx where id='"+id+"'";
			 int flag = cBean.comUp(sql);
				if(flag == Constant.SUCCESS){ 
					request.setAttribute("message", "操作成功！");
					request.getRequestDispatcher("admin/bx/index.jsp").forward(request, response); 
				}
				else { 
					request.setAttribute("message", "操作失败！");
					request.getRequestDispatcher("admin/bx/index.jsp").forward(request, response); 
				} 
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
